package com.lagou.hot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.cache.LoadingCache;
import com.lagou.hot.cache.GuavaService;
import com.lagou.hot.cache.RedisService;
import com.lagou.hot.po.TJob;
import com.lagou.hot.service.JobService;
import com.lagou.hot.util.CommUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/job")
public class JobController {

    @Autowired
    private RedisService rs;

    @Autowired
    private GuavaService gservice;

    /**
     * 从redis集群中缓存中加载数据
     * @return List<TJob>
     * @throws Exception
     */
    @RequestMapping(value = "/getRedisJobs")
    public List<TJob> getRedisJobs() throws Exception {
        System.out.println("------------------------------------------------------");
       return rs.getHotList();
    }

    /**
     * 从本地缓存中加载数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getGuavaJobs")
    public List<TJob> getGuavaJobs() throws Exception {
        //返回的数据
        List<TJob> dlist = new ArrayList<>();

        //读取列表
        Object glist = gservice.get(CommUtils.job_key,null);
        //    Object glist = cache.getIfPresent(CommUtils.job_key);
        //本地缓存有
        if (glist != null&&((List)glist).size()!=0) {
            for (Object o : (List) glist) {
                dlist.add((TJob) o); }
        }
        System.out.println("------------------------------------------------------");
        return dlist;
    }

    @RequestMapping(value = "/getJobList")
    public List<TJob> getJobList() throws Exception {
        //返回的数据
        List<TJob> dlist = new ArrayList<>();

        //读取列表
        Object glist = gservice.get(CommUtils.job_key,null);
        //    Object glist = cache.getIfPresent(CommUtils.job_key);
        //本地缓存有
        if (glist != null&&((List)glist).size()!=0) {
            for (Object o : (List) glist) {
                dlist.add((TJob) o); }
        }
        System.out.println("------------------------------------------------------");
        return dlist;
    }
}
